<template>
  <a-modal
    title="新建"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="modalOk"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="地区名称">
          <a-input v-decorator="['districtName', {rules: [{required: true, min: 1, message: '请输入地区名称'}], initialValue: model.name}]" />
        </a-form-item>
        <a-form-item label="所属地区">
          <a-select
            v-decorator="[
              'parentId',
              { rules: [{ required: true, message: '请选择地区!' }], initialValue: model.parentId },
            ]"
            placeholder="请选择地区"
            @change="areaSelectChange"
          >
            <a-select-option :value="0">
              一级地区
            </a-select-option>
            <a-select-option v-for="(item, index) in areaList" :key="index" :value="item.id">
              {{ item.districtName }}
            </a-select-option>
          </a-select>
          <!-- <a-input v-decorator="['name', {rules: [{required: true, min: 1, message: '请输入地区名称'}], initialValue: model.name}]" /> -->
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getAreaLists, addArea, putArea, getArea } from '@/api/manage'
const fields = ['id']

export default {
  name: 'AreaForm',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      areaList: [],
      form: this.$form.createForm(this)
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    const _self = this
    this.$watch('model', () => {
      if (this.model && this.model.id) {
        getArea(this.model.id).then(res => {
          this.model.parentId = res.data.parentId
        })
        getAreaLists().then(res => {
          this.areaList = res.data
          console.log(res)
        })
      }
      this.form = this.$form.createForm(this)
      _self.$nextTick(() => {
        this.model && this.form.setFieldsValue(pick(this.model, fields))
      })
    })
  },
  methods: {
    modalOk () {
      this.form.validateFieldsAndScroll((errors, values) => {
        if (!errors) {
          delete values.id
          if (this.model && this.model.id) {
            putArea(this.model.id, values).then(res => {
              this.$emit('ok')
            })
          } else {
            addArea(values).then(res => {
              this.$emit('ok')
            })
          }
        }
      })
    },
    areaSelectChange (value) {
      console.log(value)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
